<template>
  <sl-table :columns="columns" :data-source="data" row-key="productId">
    <template #bodyCell="{ record, column, index }">
      <div v-if="column.dataIndex === 'warehousingQuantity' || column.dataIndex === 'buyingPrice'">
        <a-input-number
          v-model:value="record[column.dataIndex as string]"
          :min="-9999"
          @change="numberChange(record)"
        ></a-input-number>
      </div>
      <div v-else-if="column.dataIndex === '_shelfLife' || column.dataIndex === '_productDate'">
        <a-date-picker
          v-model:value="record[column.dataIndex as string]"
          style="width: 130px"
          @change="handleDateChange($event, column.dataIndex, record)"
        ></a-date-picker>
      </div>
      <div v-else-if="column.key === 'oper'">
        <a class="text-color-danger" @click="$emit('remove', index)">删除</a>
      </div>
      <WidthFitCell v-else :title="column.title" :value="record[column.dataIndex as string]" />
    </template>
    <template v-if="summaryData" #summary>
      <a-table-summary fixed>
        <a-table-summary-row>
          <a-table-summary-cell>总计</a-table-summary-cell>
          <a-table-summary-cell :col-span="7"> </a-table-summary-cell>
          <a-table-summary-cell> {{ summaryData.orderQuantity }} </a-table-summary-cell>
          <a-table-summary-cell> {{ summaryData.warehousingQuantity }} </a-table-summary-cell>
          <a-table-summary-cell> {{ summaryData.buyingPrice }} </a-table-summary-cell>
          <a-table-summary-cell> {{ summaryData.warehousingAmount }} </a-table-summary-cell>
          <a-table-summary-cell :col-span="1"> </a-table-summary-cell>
        </a-table-summary-row>
      </a-table-summary>
    </template>
  </sl-table>
</template>
<script setup lang="ts">
import dayjs, { Dayjs } from "dayjs";
import { ProductFileItem, ReceiptItem } from "/@/api/main/pis/models";
import { ref } from "vue";
import { Api } from "/@/api/main/pis";
import { SelectExpose } from "/@/types/components";
import { message } from "ant-design-vue";
import { ReceiptTableExpose } from "./useReceiptManage";
import { cloneDeep } from "lodash-es";
import { getFloatInteger } from "/@/utils/common";
import { useReceiptTable } from "./useReceiptTable";

const props = defineProps<{
  data: Partial<ProductFileItem>[];
  summaryData?: Partial<ProductFileItem>;
  disableOper?: boolean;
}>();

const emits = defineEmits(["remove"]);

const columns = ref([
  {
    title: "商品编码",
    dataIndex: "productCode",
  },
  {
    title: "商品条码",
    dataIndex: "barCode",
  },
  {
    title: "商品名称",
    dataIndex: "name",
  },
  {
    title: "计量单位",
    dataIndex: "measurementUnit",
  },
  {
    title: "规格",
    dataIndex: "specifications",
  },
  {
    title: "商品批次",
    dataIndex: "productBatch",
  },
  {
    title: "生产日期",
    dataIndex: "_productDate",
  },
  {
    title: "截止保质期",
    dataIndex: "_shelfLife",
  },
  {
    title: "订货数量",
    dataIndex: "orderQuantity",
  },
  {
    title: "入库数量",
    dataIndex: "warehousingQuantity",
  },
  {
    title: "进货价",
    dataIndex: "buyingPrice",
  },
  {
    title: "入库金额",
    dataIndex: "warehousingAmount",
  },
]);

useReceiptTable({ props, columns });

function numberChange(record: ProductFileItem) {
  let amount = 0;
  if (record.warehousingQuantity && record.buyingPrice) {
    amount = getFloatInteger(record.warehousingQuantity * record.buyingPrice);
  }
  record.warehousingAmount = amount;
}

/**
 * 商品表格中日期变化联动
 */
function handleDateChange(value: Dayjs | string, dataIndex: string, record: ProductFileItem) {
  const validityCriteria = +record.validityCriteria;
  if (!validityCriteria) return;
  if (dataIndex === "_productDate") {
    record._shelfLife = (value as Dayjs).add(validityCriteria, "day");
  } else if (dataIndex === "_shelfLife") {
    record._productDate = (value as Dayjs).add(-validityCriteria, "day");
  }
}

defineExpose<ReceiptTableExpose>({
  getProductData() {
    const details = cloneDeep(props.data);
    for (let i = 0; i < details.length; i++) {
      const item = details[i];
      if (!item._shelfLife || !item._productDate) {
        message.info("请选择各商品的生产日期或截止保质期");
        return null;
      }
      if (!item.warehousingQuantity) {
        message.info("请输入各商品的进货数量");
        return null;
      }
      item.shelfLife = item._shelfLife?.valueOf();
      delete item._shelfLife;
      item.productDate = item._productDate?.valueOf();
      delete item._productDate;
    }
    return details;
  },
});
</script>
